<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Session;
use think\Validate;


class Work extends Controller
{
	//项目总揽
	public function worklist()
    {
    	
        $param = $this->request->get();
        $where = [];
        //设置视图查询条件
		  $table1 = ['hospital_dutyinfo'=>'a'];   //设置第1张表及表别名
		  $table2 = ['hospital_user'=>'b'];    //设置第2张表及表别名
		  // $table3 = ['hospital_proinfo'=>'c'];
		  $field2 = ['username'];  //设置第2张表字段别名
		  $on = 'a.dutyMan = b.id';   //设置2张表连接条件
		  $type = 'LEFT';    //设置连接类型为左连接
        if(!empty($param['id'])){
            $where['id'] = $param['id'];
        }
        $data = Db::view($table1)
        	->view($table2,$field2,$on,$type)
            ->order('id asc')
            ->where($where) 
            ->paginate(12);
        $this->assign('work', $data);
        return $this->fetch();
    }

    //我的项目
    public function my()
    {
        
        
        return $this->fetch();
    }

    //增加用户
    public function addWork()
    {
        $request = $this->request;

        if($request->isPost()){
            $post     = $request->post();
            $group_id = $post['group_id'];
            unset($post['group_id']);
            $validate = validate('User');
            $res      = $validate->check($post);
            if ($res !== true) {
                $this->error($validate->getError());
            } else {
                unset($post['check_password']);
                $post['password'] = md5($post['password']);
                $post['last_login_ip'] = '0.0.0.0';
                $post['create_time']   = date('Y-m-d H:i:s', time());
                $db = Db::name('user')
                    ->insert($post);
                $userId = Db::name('user')->getLastInsID();
                Db::name('auth_group_access')
                    ->insert(['uid'=>$userId,'group_id'=>$group_id]);
                $this->success('添加成功');
            }
        }else{
            $auth_group = Db::name('auth_group')
                ->field('id,title')
                ->where('status',1)
                ->order('id desc')
                ->select();
            return $this->fetch('add',['auth_group'=>$auth_group]);
        }

    }
    //删除值班记录
    public function deleteWork()
    {
        $id = $this->request->post('id');

    }
}